﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HappyTravelling.Service
{
    public static class MapExtensions
    {
        /// <summary>
        /// Allows you to set the center of a Map control using a Windows.Devices.Geolocation.Geocoordinate
        /// </summary>
        /// <param name="map">the Map control instance</param>
        /// <param name="coord">The Geocoordinate object to use to set the center of the map</param>
        public static void SetCenter(this Microsoft.Phone.Maps.Controls.Map map, Windows.Devices.Geolocation.Geocoordinate coord)
        {
            System.Device.Location.GeoCoordinate centre = new System.Device.Location.GeoCoordinate()
            {
                Latitude = coord.Latitude,
                Longitude = coord.Longitude,
            };

            map.Center = centre;
        }
    }

    public class GeoCoordinateEx
    {

        public GeoCoordinateEx(System.Device.Location.GeoCoordinate co) { GeoCoordinate = co; }        
        public GeoCoordinateEx() { GeoCoordinate = null; }
        public System.Device.Location.GeoCoordinate GeoCoordinate { get; private set; }

        public static implicit operator GeoCoordinateEx(System.Device.Location.GeoCoordinate co)
        {
            return new GeoCoordinateEx(co);
        }

        public static implicit operator GeoCoordinateEx(Windows.Devices.Geolocation.Geocoordinate coord)
        {
            return new GeoCoordinateEx(
                new System.Device.Location.GeoCoordinate()
                {
                    Altitude = coord.Altitude.HasValue ? coord.Altitude.Value : 0.0,
                    Course = coord.Heading.HasValue ? coord.Heading.Value : 0.0,
                    HorizontalAccuracy = coord.Accuracy,
                    Latitude = coord.Latitude,
                    Longitude = coord.Longitude,
                    Speed = coord.Speed.HasValue ? coord.Speed.Value : 0.0,
                    VerticalAccuracy = coord.AltitudeAccuracy.HasValue ? coord.AltitudeAccuracy.Value : 0.0,
                }
            );
        }

    }
}
